/*
* pyy Inc.
* Copyright (c) 2019 All Rights Reserved.
* create by pengyangyan
* date:2019-02-28
*/
package com.cqjtu.platform.controller;

import com.cqjtu.platform.entity.User;
import com.cqjtu.platform.enums.ExceptionEnums;
import com.cqjtu.platform.exception.MyException;
import com.cqjtu.platform.service.UserService;
import com.cqjtu.platform.utils.IpUtil;
import com.cqjtu.platform.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * User 控制器
 * 
 * @author pengyangyan
 * Date: 2019-02-28 11:15:59
 */
@Slf4j
@Api(description = "用户信息接口")
@RestController
@RequestMapping(value = "api/user")
public class UserController extends BaseController<UserService,User,Integer> {

     @Override
    @RequiresPermissions("user:add")
    public ResponseEntity<User> save(@RequestBody User entity) {
        if (StringUtils.isBlank(entity.getAccount())){
            throw new MyException(ExceptionEnums.ADD_ERROR);
        }
        if (this.service.findByAccount(entity.getAccount())!=null){
            throw new MyException(ExceptionEnums.ACCOUNT_IS_EXIT);
        }
        if (entity.getPassword()==null){
            entity.setPassword("123");
        }
        entity.setPassword(MD5Utils.encrypt(entity.getAccount(), entity.getPassword()));
        return super.save(entity);
    }

    @Override
    @RequiresPermissions("user:update")
    public ResponseEntity<User> update(@RequestBody User entity) {
         if (this.service.selectByKey(entity.getId()).getPassword().equals(entity.getPassword())){
             return super.update(entity);
         }else {
             entity.setPassword(MD5Utils.encrypt(entity.getAccount(), entity.getPassword()));
             return super.update(entity);
         }
    }

    @Override
    @RequiresPermissions("user:delete")
    @GetMapping(value = "delete/{id}")
    public ResponseEntity<String> delete(@PathVariable("id")Integer id) {
        return super.delete(id);
    }

    @ApiOperation(value = "用户登录接口")
    @RequestMapping("login")
    public ResponseEntity<User> login(@RequestBody User entity, HttpServletRequest r){
        UsernamePasswordToken token = new UsernamePasswordToken(entity.getAccount(), entity.getPassword());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            User user = (User) subject.getPrincipal();
            log.info("当前登录ip:"+IpUtil.getUserIP(r));
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            log.info("登陆失败:"+e.getMessage());
            throw e;
        }
    }

    @ApiOperation(value = "用户注销接口")
    @GetMapping("/loginOut")
    public ResponseEntity<String> loginOut(){
        Subject subject = SecurityUtils.getSubject();
        if (subject!=null){
            subject.logout();
        }
        return ResponseEntity.ok("退出登录");

    }
}
